package com.bookquery.dao.impl;

import com.bookquery.dao.BaseDao;
import com.bookquery.dao.BookQueryDao;
import com.bookquery.entity.BookQuery;
import com.bookquery.utils.DatabaseUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘俊伟
 * 图书查询系统dao层实现类
 */
public class BookQueryDaoImpl extends BaseDao implements BookQueryDao {
    public BookQueryDaoImpl(Connection conn) {
        super(conn);
    }

    /**
     * 按条件组合查询图书信息
     *
     * @param bookName   图书名称
     * @param bookAuthor 图书作者
     * @param bookType   图书类别
     * @return 查询结果
     */
    @Override
    public List<BookQuery> queryBook(String bookName, String bookAuthor, String bookType) {
        List<BookQuery> bookList = new ArrayList<BookQuery>();
        BookQuery book = null;
        ResultSet rs = null;
        String sql = "select * from bookQuery where 1=1";
        if (bookName != null&&bookName.trim().length()!=0) {
            sql += " and bookName like '%" + bookName+"%'";
        }
        if (bookAuthor != null&&bookAuthor.trim().length()!=0) {
            sql += " and bookAuthor = '" + bookAuthor+"'";
        }
        if (bookType != null&&bookType.trim().length()!=0) {
            sql += " and bookType='" + bookType+"'";
        }
        try {
            rs = this.executeQuery(sql);
            while (rs.next()) {
                book = new BookQuery();
                book.setBookAuthor(rs.getString("bookAuthor"));
                book.setBookConcern(rs.getString("bookconcern"));
                book.setBookId(rs.getInt("bookId"));
                book.setBookName(rs.getString("bookname"));
                book.setBookTime(rs.getDate("booktime"));
                book.setBookDescription(rs.getString("bookdescription"));
                book.setBookType(rs.getString("booktype"));
                bookList.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DatabaseUtil.closeAll(conn,pstmt,rs);
        }
        return bookList;
    }

    /**
     * 根据图书id查询特定图书信息
     *
     * @param bookId
     * @return
     */
    @Override
    public BookQuery queryBookById(int bookId) {
        BookQuery book = null;
        ResultSet rs = null;
        String sql = "select * from bookQuery where bookId=" + bookId;
        try {
            rs = this.executeQuery(sql);
            if (rs.next()) {
                book = new BookQuery();
                book.setBookAuthor(rs.getString("bookAuthor"));
                book.setBookConcern(rs.getString("bookconcern"));
                book.setBookId(rs.getInt("bookId"));
                book.setBookName(rs.getString("bookname"));
                book.setBookTime(rs.getDate("booktime"));
                book.setBookDescription(rs.getString("bookdescription"));
                book.setBookType(rs.getString("booktype"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DatabaseUtil.closeAll(conn,pstmt,rs);
        }
        return book;
    }
}
